<?php


namespace app\admin\controller;

use app\admin\BaseController;
use app\admin\common\Selector;
use app\admin\CommonException;
use app\admin\model\GroupPath;
use app\admin\model\RoleGroup as GroupModel;

class RoleGroup extends BaseController
{
    public function index()
    {
        $model = new GroupModel();
        $list = $model->getList($this->request);
        $select = [
            'id' => '权限组编号',
            'group_name' => '权限组名称'
        ];
        $selector = Selector::render($select, 'search', $this->request->get('search', ''));
        return view('rolegroup/index', [
            'list' => $list->toArray()['data'] ?? [],
            'page' => $list->__toString(),
            'selector' => $selector
        ]);
    }

    /**
     * @return bool|\think\response\View
     * @throws CommonException
     */
    public function add()
    {
        $data = $this->request->post();
        if (!$data) {
            return view('/rolegroup/add', ['url' => '/rolegroup/add']);
        }

        $validate = new \app\validate\RoleGroup();

        $result = $validate->check($data);
        if (!$result) error($validate->getError(), '', 10086);


        //新增
        $model = new GroupModel();
        $model->data($data, true);
        $res = $model->save();
        if ($res) {
            $this->success('添加成功!', '/rolegroup/add');
        }
        error('添加失败!请稍候再试');
        return false;
    }

    /**
     * 删除
     * @throws
     */
    public function del()
    {
        $model = new GroupModel();
        $id = $this->request->get('id', '');
        if (!$id) return false;
        $role = $model->find($id);
        if (!$role) return false;
        return $role->delete();
    }

    /**
     * 检查角色名唯一
     */
    public function checkName()
    {
        $name = $this->request->post('group_name');
        $id = $this->request->post('id', 0);
        $validate = new \app\validate\RoleGroup();
        $data = ['group_name' => $name];
        if ($id) $data['id'] = $id;
        $result = $validate->check($data);
        return $result;
    }

    /**
     * @return \think\response\View
     * @throws
     */
    public function update()
    {
        $data = $this->request->post();
        if (!$data) {
            $id = $this->request->request('id', '');
            if (!$id) throw new CommonException('缺少参数!', '/rolegroup/index');
            $role = GroupModel::find($id);
            if (!$role) throw new CommonException('请刷新后重试!', '/rolegroup/index');
            return view('rolegroup/add', ['data' => $role, 'url' => '/rolegroup/update']);
        }

        //修改
        $role = GroupModel::find($data['id']);
        if (!$role) throw new CommonException('请刷新后重试!', '/rolegroup/index');

        $validate = new \app\validate\role();
        $result = $validate->check($data);
        if (!$result) error($validate->getError(), '', 10086);

        //修改
        $role->data($data, true);
        $res = $role->save();
        if ($res) {
            $this->success('修改成功!', '/rolegroup/update.html?id='.$data['id']);
        }
        error('修改失败!请稍候再试');
    }

    /**
     * 权限组添加节点 页面
     * @throws
     */
    public function addPath(){
        $id = $this->request->request('id', '');
        if (!$id) throw new CommonException('缺少参数!', '/rolegroup/index');
        $role = GroupModel::find($id);
        if (!$role) throw new CommonException('请刷新后重试!', '/rolegroup/index');
        $paths = GroupModel::getGroupPaths($id);

        return view('rolegroup/addpath',['data' => $role,'paths'=>$paths]);
    }

    /**
     * 添加组节点
     * @throws
     */
    public function addGroupPath(){
        $groupId = $this->request->request('group_id');
        $pathId = $this->request->request('path_id');
        if($groupId && $pathId){
            //保存权限组没有的节点
            $bool = GroupPath::where('group_id',$groupId)->where('path_id',$pathId)->find();
            if(!$bool){
                $Model = new GroupPath();
                if($Model->data(['group_id'=>$groupId,'path_id'=>$pathId])->save()){
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 删除组节点
     * @throws
     */
    public function delGroupPath(){
        $groupId = $this->request->request('group_id');
        $pathId = $this->request->request('path_id');
        if($groupId && $pathId){
            $bool = GroupPath::where('group_id',$groupId)->where('path_id',$pathId)->find();
            if($bool){
                if(GroupPath::where(['group_id'=>$groupId,'path_id'=>$pathId])->delete()){
                    return true;
                }
            }
        }
        return false;
    }

}